package com.kai.controller;

import com.kai.pojo.NameValue;
import com.kai.service.CoachService;
import com.kai.service.CommentService;
import com.kai.service.UserService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class EchartsController {
    @Autowired
    private UserService userService;
    @Autowired
    private CoachService coachService;
    @Autowired
    private CommentService commentService;

    /*饼图*/
    @GetMapping("/selectUserAndCoachCount")
    public List<NameValue> selectUserAndCoachCount(){
        List<NameValue> nameValues = new ArrayList<>();
        NameValue nameValue = new NameValue();
        int i = userService.selectAllUserCount(null, null);
        nameValue.setName("教练总数");
        nameValue.setValue(i);
        nameValues.add(nameValue);
        int i1 = coachService.selectAllCoachCount(null, null);
        NameValue nameValue1 = new NameValue();
        nameValue1.setName("用户总数");
        nameValue1.setValue(i1);
        nameValues.add(nameValue1);
        return nameValues;
    }
    /*折线图*/
    @GetMapping("/selectAddUserCount")
    public List<NameValue> selectAddUserCount(){
        List<NameValue> list = userService.selectAddUserCount();
        return list;
    }
    /*统计新增用户，教练人数，新增留言*/
    @GetMapping("/selectMain")
    public Map selectMain(){
        Map<String, Object> map = new HashMap<>();
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        //ca.add(Calendar.YEAR, -1); //年份减1
        // ca.add(Calendar.MONTH, -1)，//求前一月
        ca.add(Calendar.DATE, -1); //前一天
        Date lastMonth = ca.getTime(); //结果
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String s = simpleDateFormat.format(lastMonth);
        int addUsersPerDay = userService.addUsersPerDay(s);
        int coachCount = coachService.selectAllCoachCount(null, null);
        int theNewMessage = commentService.annuallyComment(s);
        map.put("addUsersPerDay",addUsersPerDay);
        map.put("coachCount",coachCount);
        map.put("theNewMessage",theNewMessage);
        return map;
    }
}
